<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StoreHead extends Model
{
    use HasFactory;

    protected $guarded = [];
    protected $table = 'store_head';


    /**
     * 团长申请
     * @param $arr
     * @return mixed
     */
    public static function createHead($arr){
        return self::create($arr);
    }

    /**
     * 团长列表
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public static function headList(){
        return self::with(['users','cities'])->paginate(10);
    }

    /**
     * 团长和用户关系
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function users(){
        return $this->belongsTo(User::class,'id','uid');
    }

    /**
     * 团长和城市关系
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function cities(){
        return $this->belongsTo(SystemCity::class,'city_id','id');
    }

    /**
     * 团长状态列表
     * @param $status
     * @return mixed
     */
    public static function headStatus($status){
        return self::with(['users','cities'])->where('status',$status)->paginate(10);
    }

    /**
     * 搜索
     * @param $start_at
     * @param $end_at
     * @param $keyword
     * @param $status
     * @return mixed
     */
    public static function search($start_at,$end_at,$keyword,$status){
        return self::with(['users','cities'])
            ->where('name','like',"%$keyword%")
            ->where('status',$status)
            ->whereDate('created_at','>=',$start_at)
            ->whereDate('created_at','<=',$end_at)
            ->get();
    }

    /**
     * 通过不通过
     * @param $id
     * @param $status
     * @return mixed
     */
    public static function adopt($id,$status){
        return self::where('id',$id)->update(['status'=>$status]);
    }

    /**
     * 查询单条团长信息
     * @param $id
     * @return mixed
     */
    public static function headFind($id){
        return self::where('id',$id)->first();
    }

    /**
     * 团长修改
     * @param $params
     * @return bool
     */
    public static function updateHead($params){
        return self::where('id',$params['id'])->update($params);
    }

    public static function getHeadId($headName){
        return self::where("name","like","$headName%")->get("id")->toArray();
    }
}
